<<<<<<< HEAD
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Accountcash extends MY_Controller {
		
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
		$admin = $this->session->userdata('admin_user');
		if(!$admin || !isset($admin['user_id'])){
			header('Location: /sys_admin/login');
			exit;
		}
		$this->tab  ='account';
	}
	
	function zhs_accountcashlist(){
		$per_page 	= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$username 	= $this->input->get('username');
		$realname 	= $this->input->get('realname');
		$is_loan 	= isset($_GET['is_loan']) ? $this->input->get('is_loan') : -1;//$this->input->get('is_loan');
		$fstatus 	= isset($_GET['fstatus']) ? $this->input->get('fstatus') : -1;//$this->input->get('fstatus');
		$strtime	= $this->input->get('strtime');
		$endtime	= $this->input->get('endtime');
		
		$data = array();
		if($username != ''){
			$data['where']['username'] = $username;
		}
		if($realname != ''){
			$data['where']['realname'] = $realname;
		}
		if($is_loan != '' && $is_loan != -1){
			$data['where']['is_loan'] = $is_loan;
		}
		if($fstatus != '' && $fstatus != -1){
			$data['where']['fstatus'] = $fstatus;
		}
		if($strtime != ''){
			$data['where']['strtime'] = $strtime;
		}
		if($endtime != ''){
			$data['where']['endtime'] = $endtime;
		}
		
		$data['page_size']  = sys_admin_LIST_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$this->load->model('Accountcash_model');
		$vdata = $this->Accountcash_model->get_accountcash_list($data);
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/accountcash/zhs_accountcashlist/?username='.$username.'&realname='.$realname.'&is_loan='.$is_loan.'&fstatus='.$fstatus.'&strtime='.$strtime.'&endtime='.$endtime;
		$config['total_rows'] = $vdata['accountcash_count'];
		$config['per_page'] = sys_admin_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		
		$this->load->model('Area_model', 'area');
		$vdata['area_list'] = $this->area->get_list();
		$this->load->model('Accountcash_model');
		$vdata['account_bank'] 			= $this->Accountcash_model->arrays("account_bank");
		$vdata['user_loan'] 			= $this->Accountcash_model->arrays("user_loan");
		$vdata['account_cash_status'] 	= $this->Accountcash_model->arrays("account_cash_status");
		$vdata['username'] 	= $username;
		$vdata['realname'] 	= $realname;
		$vdata['is_loan'] 	= $is_loan;
		$vdata['fstatus'] 	= $fstatus;
		$vdata['strtime'] 	= $strtime;
		$vdata['endtime'] 	= $endtime;
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'accountcash';
		
		$this->load->view('/sys_admin/account/accountcash_list', $vdata);
	}
	
	function accountcashexcel(){
		$username 	= $this->input->get('username');
		$realname 	= $this->input->get('realname');
		$is_loan 	= $this->input->get('is_loan');
		$fstatus 	= $this->input->get('fstatus');
		$strtime	= $this->input->get('strtime');
		$endtime	= $this->input->get('endtime');
		
		$data = array();
		if($username != ''){
			$data['where']['username'] = $username;
		}
		if($username != ''){
			$data['where']['realname'] = $realname;
		}
		if($is_loan != '' && $is_loan != -1){
			$data['where']['is_loan'] = $is_loan;
		}
		if($fstatus != '' && $fstatus != -1){
			$data['where']['fstatus'] = $fstatus;
		}
		if($strtime != ''){
			$data['where']['strtime'] = $strtime;
		}
		if($endtime != ''){
			$data['where']['endtime'] = $endtime;
		}
		$data['limit'] = 'all';
		$data['page_size']  = sys_admin_LIST_PAGE_SIZE;
		$data['page_start'] = 0;
		$this->load->model('Accountcash_model');
		$vdata = $this->Accountcash_model->get_accountcash_list($data);
		
		$this->load->model('Area_model', 'area');
		$area_list = $this->area->get_list();
		//print_r($area_list);return;
		
		$this->load->library('PHPExcel');
		$this->load->library('PHPExcel/IOFactory'); 
		
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setTitle("用户资金统计")->setDescription(COMPANY_NAME); 
		$user_loan 				 = $this->Accountcash_model->arrays("user_loan");
		$account_bank 				 = $this->Accountcash_model->arrays("account_bank");
		$account_cash_status 				 = $this->Accountcash_model->arrays("account_cash_status");
		$objPHPExcel->setActiveSheetIndex(0);
		$titles = array("ID", "用户名", "姓名", "用户类型", "银行", "省份", "城市", "开户行","帐号", "提现金额", "到账金额", 
						"手续费", "申请时间", "审核时间", "审核备注", "状态");
		
		for($i = 0; $i < count($titles); $i ++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $titles[$i]);
		}
		$accountcash_list = $vdata['accountcash_list'];
		for($i = 0; $i < count($accountcash_list); $i++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i+2, $accountcash_list[$i]['id']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $i+2, $accountcash_list[$i]['username']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $i+2, $accountcash_list[$i]['realname']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $i+2, $user_loan[$accountcash_list[$i]['is_loan']]);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $i+2, @$account_bank[$accountcash_list[$i]['bank']]);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $i+2, $accountcash_list[$i]['province']?$area_list[$accountcash_list[$i]['province']]:'');
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6, $i+2, $accountcash_list[$i]['city']?$area_list[$accountcash_list[$i]['city']]:'');
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(7, $i+2, $accountcash_list[$i]['branch']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(8, $i+2, $accountcash_list[$i]['account'].' ');
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(9, $i+2, $accountcash_list[$i]['total']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(10, $i+2, $accountcash_list[$i]['credited']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(11, $i+2, $accountcash_list[$i]['fee']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(12, $i+2, date('Y-m-d H:i:s', $accountcash_list[$i]['add_time']));
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(13, $i+2, date('Y-m-d H:i:s', $accountcash_list[$i]['verify_time']));
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(14, $i+2, $accountcash_list[$i]['verify_remark']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(15, $i+2, $account_cash_status[$accountcash_list[$i]['fstatus']]);
		}
		
		
		$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); 
	
		//发送标题强制用户下载文件 
		header('Content-Type: application/vnd.ms-excel'); 
		header('Content-Disposition: attachment;filename="用户提现.xls"'); 
		header('Cache-Control: max-age=0'); 
		
		$objWriter->save('php://output'); 
	}
	
	function verify(){
		$this->load->library('session');
		$adminUser = $this->session->userdata('admin_user');
		if(!isset($adminUser['user_id']) || !$adminUser['user_id']){
			echo "非法操作";
			exit;
		}
		$id 			= $this->input->post("id");
		$fstatus 		= $this->input->post("fstatus");
		$verify_remark	= $this->input->post("verify_remark");
		$this->load->model('Accountcash_model');
		$this->load->model('Account_model');
		$cash = $this->Accountcash_model->get_by_id($id);
		if($cash){
			if($cash['fstatus']!=0 || $cash['fstatus']==$fstatus){
				echo "请不要重复操作";
				exit;
			}
			if($fstatus == 2){
				$user_account = $this->Account_model->get_account($cash['user_id']);
				$log = array(
	  				'user_id' => $cash['user_id'],
	  				'ftype' => RECHARGE_FALSE,//提现失败
					'money' => $cash['total'],
	  				'total' => $user_account['total'],
	  				'use_money' => $user_account['use_money']+$cash['total'],
	  				'no_use_money' => $user_account['no_use_money']-$cash['total'],
	  				'collection' => $user_account['collection'],
	  				'to_user' => 1,
	  				'remark' => '提现审核未通过',
	  				'add_time' => time(),
	  				'addip' => $this->js->get_ip(),
	  			);
	  			$ret = $this->Account_model->addlog($log);
			}
			$data["fstatus"]			= $fstatus;
			$data["verify_userid"]      = $adminUser['user_id'];
			$data["verify_remark"]		= $verify_remark;
			$data["verify_time"]		= time();
			$data["verify_ip"]			= $this->js->get_ip();
			$ret = $this->Accountcash_model->update($id, $data);
			$this->load->model('log_model');
			$this->log_model->add_log('[审核][提现][ID=='.$id.']');
			echo "操作成功";
		}else{
			$this->load->model('log_model');$this->log_model->add_log('[审核][提现][错误][ID=='.$id.']');
			echo "数据错误";
		}
		
		/***
		$this->load->library('session');
		$adminUser = $this->session->userdata('admin_user');
		if(!isset($adminUser['user_id']) || !$adminUser['user_id']){
			echo "非法操作";
			exit;
		}
		$id 			= $this->input->post("id");
		$fstatus 		= $this->input->post("fstatus");
		$verify_remark	= $this->input->post("verify_remark");
		$this->load->model('Accountcash_model');
		$this->load->model('Account_model');
		$cash = $this->Accountcash_model->get_by_id($id);
		$this->load->model('User_model','user');
		$user_account = $this->Account_model->get_account($cash['user_id']);
		if($cash['fstatus'] == 0 && $fstatus  == 1 ){
			
			// ******* 20141227 by liub  资金托管：向宝付提现申请
			$this->load->library('baofooSdk/BaofooRequestService',0,'br');
			
			$user_msg = $this->user->get_realname_by_id($cash['user_id']);
			$config = Array();
			// 封装请求信息
			$config['order_id'] 	= self::createOrderId(6);// 订单ID(必须唯一)
			$config['user_id'] 		= $cash['user_id'];
			$config['user_name'] 	= $user_msg['realname'];
			$config['amount'] 		= $cash['total']-$cash['fee'];
			$config['fee'] 			= $cash['fee'];
			$config['fee_taken_on'] = FEE_TAKEN_ON;
			$config['bank_no'] 		= $cash['account'];
			
			$this->load->library('baofooSdk/dto/request/FoChargeDto',$config,'foChargeDto');
			$result 		= 	$this->br->	api_FoCharge($this->foChargeDto);	
		
			if($result['code'] !='CSD000'){
				$this->db->trans_strict(FALSE);
    			$this->db->trans_begin();
				
				echo "提现订单失败，请联系管理员";
				$data["fstatus"]			= 6;// 申请失败
				$data["verify_userid"]      = $adminUser['user_id'];
				$data["verify_remark"]		= $result['msg'];
				$data["verify_time"]		= time();
				$data["trade_no"]			= $config['order_id'];// 订单号
				$data["bf_result_code"]		= $result['code'];		// 宝付失败返回码
				$data["verify_ip"]			= $this->js->get_ip();
				$ret = $this->Accountcash_model->update($id, $data);
				
				$log = array(
	  				'user_id' => $cash['user_id'],
	  				'ftype' => RECHARGE_FALSE,//提现失败
					'money' => $cash['total'],
	  				'total' => $user_account['total'],
	  				'use_money' => $user_account['use_money']+$cash['total'],
	  				'no_use_money' => $user_account['no_use_money']-$cash['total'],
	  				'collection' => $user_account['collection'],
	  				'to_user' => 1,
	  				'remark' => '提现审核未通过',
	  				'add_time' => time(),
	  				'addip' => $this->js->get_ip(),
	  			);
	  			$ret = $this->Account_model->addlog($log);
				if ($this->db->trans_status() === TRUE) {
        			$this->db->trans_commit();
        			return 1;
   				 } else {
       		 		$this->db->trans_rollback();
       				 //@todo 异常处理部分
        			return 0;
   			 	}
			}
		}
		if($cash){
			if($cash['fstatus']!=0 || $cash['fstatus']==$fstatus){
				echo "请不要重复操作";
				exit;
			}
			$this->db->trans_strict(FALSE);
    		$this->db->trans_begin();
			if($fstatus == 2){
				$log = array(
	  				'user_id' => $cash['user_id'],
	  				'ftype' => RECHARGE_FALSE,//提现失败
					'money' => $cash['total'],
	  				'total' => $user_account['total'],
	  				'use_money' => $user_account['use_money']+$cash['total'],
	  				'no_use_money' => $user_account['no_use_money']-$cash['total'],
	  				'collection' => $user_account['collection'],
	  				'to_user' => 1,
	  				'remark' => '提现审核未通过',
	  				'add_time' => time(),
	  				'addip' => $this->js->get_ip(),
	  			);
	  			$ret = $this->Account_model->addlog($log);
			}
			$data["fstatus"]			= $fstatus;
			$data["verify_userid"]      = $adminUser['user_id'];
			$data["verify_remark"]		= $verify_remark;
			$data["verify_time"]		= time();
			$data["trade_no"]			= $config['order_id'];// 订单号
			$data["verify_ip"]			= $this->js->get_ip();
			$ret = $this->Accountcash_model->update($id, $data);
			$this->load->model('log_model');
			$this->log_model->add_log('[审核][提现][ID=='.$id.']');
			
			$ret = $this->Account_model->addlog($log);
			if ($this->db->trans_status() === TRUE) {
        		$this->db->trans_commit();
        		echo "操作成功";
        		return 1;
   			} else {
       		 	$this->db->trans_rollback();
       				//@todo 异常处理部分
       			echo "操作失败";
        		return 0;
   			 }
			
		}else{
			$this->load->model('log_model');$this->log_model->add_log('[审核][提现][错误][ID=='.$id.']');
			echo "数据错误";
		}
		*/
	}
	
	/** 到帐审核*/
	function review(){
		$id 			= $this->input->post("id");
		$fstatus 		= $this->input->post("fstatus");
		$review_remark	= $this->input->post("review_remark");
		
		$data["fstatus"]			= $fstatus;
  		$data["review_remark"]		= $review_remark;
  		$data["review_time"]		= time();
  		$data["review_ip"]			= $this->js->get_ip();
  		$this->load->model('Accountcash_model');
  		$this->load->model('Account_model');
  		$cash_obj = $this->Accountcash_model->get_by_id($id);
  		$time = time();
  		$ip = $this->js->get_ip();
		if($cash_obj['fstatus'] == 1){
			// 事务开启
			$this->db->trans_strict(FALSE);
   			$this->db->trans_begin();
			$ret = $this->Accountcash_model->update($id, $data);
			$cash = $this->Accountcash_model->get_by_id($id);
			if($ret){
	  			$account = $this->Account_model->get_account($cash['user_id']);
	  			if($fstatus == 4){
	  				$log = array(
		  				'user_id' => $cash['user_id'],
		  				'ftype' => RECHARGE_SUCCESS,// 到帐
		  				'total' => $account['total']-$cash['total'],
		  				'money' => $cash['total'],
		  				'use_money' => $account['use_money'],
		  				'no_use_money' => $account['no_use_money']-$cash['total'],
		  				'collection' => $account['collection'],
		  				'to_user' => 1,
		  				'remark' => '提现审核通过',
		  				'add_time' => $time,
		  				'addip' => $ip,
		  			);
		  			$ret = $this->Account_model->addlog($log);
		  			if ($ret){
		  				//提醒设置
		  				$this->notice_message($log['user_id'], '提现审核通过', '您于 '.date('Y-m-d H:i:s').' 提现：'.$cash['total'].'元审核通过，到账：'.$cash['credited'].'元，手续费：'.$cash['fee'].'元。');
						
		  			}
	  			}elseif($fstatus == 2){
	  				$log = array(
		  				'user_id' => $cash['user_id'],
		  				'ftype' => RECHARGE_FALSE,//提现失败
		  				'total' => $account['total'],
		  				'money' => $cash['total'],
		  				'use_money' => $account['use_money']+$cash['total'],
		  				'no_use_money' => $account['no_use_money']-$cash['total'],
		  				'collection' => $account['collection'],
		  				'to_user' => 1,
		  				'remark' => '提现到帐失败',
		  				'add_time' => $time,
		  				'addip' => $ip,
		  			);
		  			$ret = $this->Account_model->addlog($log);
	  			}
	  		}
	  		
			if ($this->db->trans_status() === TRUE) {
        		$this->db->trans_commit();
        		$this->load->model('log_model');$this->log_model->add_log('[审核][到账][成功][ID=='.$id.']');
        		echo "1";
    		} else {
      		 	 $this->db->trans_rollback();
      		 	 $this->load->model('log_model');$this->log_model->add_log('[审核][到账][失败][ID=='.$id.']');
      		 	 echo "操作失败！";
   			}
		}
	}
		
		/****
		$id 			= $this->input->post("id");
		$fstatus 		= $this->input->post("fstatus");
		$review_remark	= $this->input->post("review_remark");
	  	$this->load->model('Accountcash_model');
  		$this->load->model('Account_model');
		$cash_obj = $this->Accountcash_model->get_by_id($id);
	
		 //TODO 资金托管：验证
		$config['order_id'] = $cash_obj['trade_no'];
		$config['type'] = "6";
		$config['start_time'] = null;
		$config['end_time'] = null;
		$this->load->library('baofooSdk/dto/request/P2pQueryDto',$config,'P2pQueryDto');
		$this->load->library('baofooSdk/BaofooRequestService',0,'br');
		$resule = $this->br->api_P2pQuery($this->P2pQueryDto);
		$state = 0;// 0 未知，1 成功
		
		if($resule['result'] == null ){
			echo '宝付查询无此充值订单';
			exit;
		}
		if($resule['code'] == "CSD000"){
			if($resule['result'][0]['order'][0]['order_id'] == $config['order_id']  ){
				$state = $resule['result'][0]['order'][0]['state'];
			}else{
				echo '宝付查询无此提现订单';
				exit;
			}
		}else{
			echo '宝付查询失败，返回码：'.$resule['code'];
			exit;
		}
		if($state == -1){
						$time = time();
  			$ip = $this->js->get_ip();
			$this->load->model('Account_model');
  			$this->load->model('Accountcash_model');
  			$this->load->model('Account_model');
  			
  			// 事务开启
			$this->db->trans_strict(FALSE);
   			$this->db->trans_begin();

				$data["fstatus"]				= 6;// 提现失败
  			$data["review_remark"]	= "宝付：提现已失败";
  			$data["review_time"]		= $time;
  			$data["review_ip"]			= $ip;			
				$ret = $this->Accountcash_model->update($id, $data);
  			if($ret){
  				$cash = $this->Accountcash_model->get_by_id($id);
  				$account = $this->Account_model->get_account($cash['user_id']);
			 		$log = array(
			  			'user_id' => $cash['user_id'],
			  			'ftype' => RECHARGE_FALSE,//提现失败
			  			'total' => $account['total'],
			  			'money' => $cash['total'],
			  			'use_money' => $account['use_money']+ $cash['total'],
			  			'no_use_money' => $account['no_use_money']-$cash['total'],
			  			'collection' => $account['collection'],
			  			'to_user' => $cash['user_id'],
			  			'remark' => '宝付：提现已失败',
			  			'add_time' => $time,
			  			'addip' => $ip,
			  		);
			  	$return = $this->Account_model->addlog($log);
			  	if ($return){
			  		//提醒设置
					//$this->load->model('Redmanagement_model', 'redmanagement');
					//$this->redmanagement->send_red($cash['user_id'],9,"提现");
			  		$this->notice_message($log['user_id'], '宝付：提现已失败', '您于 '.date('Y-m-d H:i:s').' 提现：'.$cash['total'].'元失败，返账：'.$cash['credited'].'元。');
			  	}
  			}
  			// 事务提交
			if($this->db->trans_status() === TRUE) {
        		$this->db->trans_commit();
        		$this->load->model('log_model');
        		$this->log_model->add_log('[自动审核][提现到账][失败][ID=='.$id.']');
        		echo '宝付反馈结果:提现失败';
				exit;
    		} else {
      		 	$this->db->trans_rollback();
      		 	$this->load->model('log_model');
      		 	$this->log_model->add_log('[自动审核][提现到账][失败][ID=='.$id.']');
        		echo '宝付反馈结果:未知';
				exit;
   			}
   			
		}elseif($state == 5){
			$data["fstatus"]			= 7;
  			$data["review_remark"]		= '宝付反馈结果:转账中';
  			$data["review_time"]		= time();
  			$data["review_ip"]			= $this->js->get_ip();
				$ret = $this->Accountcash_model->update($id, $data);
				echo '宝付反馈结果:转账中';
				exit;
		}elseif($state == 0){
			echo '宝付反馈结果:未知';
			exit;
		}
		
		$data["fstatus"]			= 4;
  		$data["review_remark"]		= $review_remark;
  		$data["review_time"]		= time();
  		$data["review_ip"]			= $this->js->get_ip();
  	
  		$this->load->model('Account_model');
  		$time = time();
  		$ip = $this->js->get_ip();
  		
		if($cash_obj['fstatus'] == 1){
			
			 $this->db->trans_strict(FALSE);
   			 $this->db->trans_begin();
   			 
			$ret = $this->Accountcash_model->update($id, $data);
			$cash = $this->Accountcash_model->get_by_id($id);
			if($ret){
	  			$account = $this->Account_model->get_account($cash['user_id']);
	  			if($fstatus == 4){
	  				$log = array(
		  				'user_id' => $cash['user_id'],
		  				'ftype' => RECHARGE_SUCCESS,//提现审核通过
		  				'total' => $account['total']-$cash['total'],
		  				'money' => $cash['total'],
		  				'use_money' => $account['use_money'],
		  				'no_use_money' => $account['no_use_money']-$cash['total'],
		  				'collection' => $account['collection'],
		  				'to_user' => 1,
		  				'remark' => '提现成功',
		  				'add_time' => $time,
		  				'addip' => $ip,
		  			);
		  			$ret = $this->Account_model->addlog($log);
		  			if ($ret){
		  				//提醒设置
		  				//发放代金券
						$this->load->model('Redmanagement_model', 'redmanagement');
						$this->redmanagement->send_red($cash['user_id'],9,"提现");
		  				$this->notice_message($log['user_id'], '提现审核通过', '您于 '.date('Y-m-d H:i:s').' 提现：'.$cash['total'].'元审核通过，到账：'.$cash['credited'].'元，手续费：'.$cash['fee'].'元。');
						
		  			}
	  			}elseif($fstatus == 2){
	  				$log = array(
		  				'user_id' => $cash['user_id'],
		  				'ftype' => RECHARGE_FALSE,//提现失败
		  				'total' => $account['total'],
		  				'money' => $cash['total'],
		  				'use_money' => $account['use_money']+$cash['total'],
		  				'no_use_money' => $account['no_use_money']-$cash['total'],
		  				'collection' => $account['collection'],
		  				'to_user' => 1,
		  				'remark' => '提现审核未通过',
		  				'add_time' => $time,
		  				'addip' => $ip,
		  			);
		  			$ret = $this->Account_model->addlog($log);
	  			}
	  		}
	  		
	  		if ($this->db->trans_status() === TRUE) {
        		$this->db->trans_commit();
        		$this->load->model('log_model');$this->log_model->add_log('[审核][到账][成功][ID=='.$id.']');
        		echo "1";
        		return 1;
    		} else {
      		 	 $this->db->trans_rollback();
      		 	 $this->load->model('log_model');$this->log_model->add_log('[审核][到账][失败][ID=='.$id.']');
      		 	 echo "操作失败！";
        		//@todo 异常处理部分
        		return 0;
   			}
		}
		*/
	

	function batchreview(){
		$ids 			= $this->input->post("ids");
		$id_list = explode(',', $ids);
		$fstatus = 4;
		$ip = $this->js->get_ip();
		$data["fstatus"]			= $fstatus;
  		$data["review_remark"]		= "审核通过";
  		$data["review_time"]		= time();
  		$data["review_ip"]			= $ip;
  		$this->load->model('Accountcash_model');
  		$this->load->model('Account_model');
  		$this->load->model('Message_model');
  		$time = time();
		foreach ($id_list as $id){
			$cash_obj = $this->Accountcash_model->get_by_id($id);
			if($cash_obj['fstatus'] == 1){
				$this->db->trans_strict(FALSE);
				$this->db->trans_begin();//事务开始
				$ret = $this->Accountcash_model->update($id, $data);
				$cash = $this->Accountcash_model->get_by_id($id);
				if($ret){
		  			$account = $this->Account_model->get_account($cash['user_id']);
		  			if($fstatus == 4){
		  				$log = array(
			  				'user_id' => $cash['user_id'],
			  				'ftype' => RECHARGE_SUCCESS,//提现审核通过
			  				'total' => $account['total']-$cash['total'],
			  				'money' => $cash['total'],
			  				'use_money' => $account['use_money'],
			  				'no_use_money' => $account['no_use_money']-$cash['total'],
			  				'collection' => $account['collection'],
			  				'to_user' => 1,
			  				'remark' => '提现审核通过',
			  				'add_time' => $time,
			  				'addip' => $ip
			  			);
			  			$ret = $this->Account_model->addlog($log);
			  			if ($ret){
			  				//提醒设置
			  				$this->notice_message($log['user_id'], '提现审核通过', '您于 '.date('Y-m-d H:i:s').' 提现：'.$cash['total'].'元审核通过，到账：'.$cash['credited'].'元，手续费：'.$cash['fee'].'元。');
							
			  			}
		  			}elseif($fstatus == 2){
		  				$log = array(
			  				'user_id' => $cash['user_id'],
			  				'ftype' => RECHARGE_FALSE,//提现失败
			  				'total' => $account['total'],
			  				'money' => $cash['total'],
			  				'use_money' => $account['use_money']+$cash['total'],
			  				'no_use_money' => $account['no_use_money']-$cash['total'],
			  				'collection' => $account['collection'],
			  				'to_user' => 1,
			  				'remark' => '提现审核未通过',
			  				'add_time' => $time,
			  				'addip' => $ip
			  			);
			  			$ret = $this->Account_model->addlog($log);
		  			}
		  		}
				if($this->db->trans_status() === TRUE){
					$this->db->trans_commit();
				}else{
					$this->db->trans_rollback();
		  			echo "操作失败！";
		  			exit;
				}
			}
			$ret = 0;
		}		
		echo "1";
	}
	

	function deduct_view(){
		$this->load->model('Accountlog_model');
		$var_account_deduct_type = $this->Accountlog_model->arrays("var_account_deduct_type");
		$vdata['var_account_deduct_type'] = $var_account_deduct_type;
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'deduct';
		
		$this->load->view('/sys_admin/account/accountdeduct', $vdata);
	}

	
	function deduct(){
		$ret = array(
			'msg' => '',
			'url' => '',
		);
		$username 		= $this->input->post('username');
		$money 			= $this->input->post('money');
		$deduct_type 	= $this->input->post('deduct_type');
		$remark 		= $this->input->post('remark');
		
		$user_id = 0; 
		if(!$username){
			$ret['msg'] = '请填写用户名';
		}elseif(!$this->myformcheck->IsDouble($money)){
			$ret['msg'] = '请填写有效的金额';
		}elseif(!$remark){
			$ret['msg'] = '请填写备注';
		}else{
			$this->load->model('User_model');
			$user_id = $this->User_model->get_userid_by_username($username);
			if($user_id < 1){
				$ret['msg'] = '用户名不存在';
			}
		}
		if($ret['msg']){
			echo json_encode($ret);exit;
		}
		$admin = $this->session->userdata('admin_user');
		$data['admin_user_id']  = $admin['user_id'];
		$data['user_id'] 		= $user_id;
		$data['account'] 		= $money;
		$data['ftype'] 	 		= $deduct_type;
		$data['remark']  		= $remark;
		$data['ip']  			= $this->js->get_ip();
		
		$this->load->model('Accountcash_model');
		$flag = $this->Accountcash_model->deduct($data);
		if($flag == 1){
			$ret['msg'] = '费用扣除成功';
			$this->load->model('log_model');$this->log_model->add_log('[添加][费用扣除]'.'[用户名:'.$username.',金额:'.$money.']');
		}else{
			$ret['msg'] = '费用扣除失败';
			$this->load->model('log_model');$this->log_model->add_log('[添加][费用扣除]'.'[用户名:'.$username.',金额:'.$money.']');
		}
   		echo json_encode($ret);
	}
	
	
	//***** by liub 根据交易类型生成订单ID
	private function createOrderId($type){
		// 时间戳
		$orderid = time();
		// 生成3位随机数字
		$num = create_num(3);// 模拟毫秒数
		// 生成订单id
		$orderid =$type.'_'.$orderid.$num;
		
		return $orderid;
	}
=======
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Accountcash extends MY_Controller {
    	
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
		$admin = $this->session->userdata('admin_user');
		if(!$admin || !isset($admin['user_id'])){
			header('Location: /sys_admin/login');
			exit;
		}
		$this->tab  ='account';
	}
	
	function zhs_accountcashlist(){
		$per_page 	= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$username 	= $this->input->get('username');
		$realname 	= $this->input->get('realname');
		$is_loan 	= isset($_GET['is_loan']) ? $this->input->get('is_loan') : -1;//$this->input->get('is_loan');
		$fstatus 	= isset($_GET['fstatus']) ? $this->input->get('fstatus') : -1;//$this->input->get('fstatus');
		$strtime	= $this->input->get('strtime');
		$endtime	= $this->input->get('endtime');
		
		$data = array();
		if($username != ''){
			$data['where']['username'] = $username;
		}
		if($realname != ''){
			$data['where']['realname'] = $realname;
		}
		if($is_loan != '' && $is_loan != -1){
			$data['where']['is_loan'] = $is_loan;
		}
		if($fstatus != '' && $fstatus != -1){
			$data['where']['fstatus'] = $fstatus;
		}
		if($strtime != ''){
			$data['where']['strtime'] = $strtime;
		}
		if($endtime != ''){
			$data['where']['endtime'] = $endtime;
		}
		
		$data['page_size']  = sys_admin_LIST_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$this->load->model('Accountcash_model');
		$vdata = $this->Accountcash_model->get_accountcash_list($data);
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/accountcash/zhs_accountcashlist/?username='.$username.'&realname='.$realname.'&is_loan='.$is_loan.'&fstatus='.$fstatus.'&strtime='.$strtime.'&endtime='.$endtime;
		$config['total_rows'] = $vdata['accountcash_count'];
		$config['per_page'] = sys_admin_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		
		$this->load->model('Area_model', 'area');
		$vdata['area_list'] = $this->area->get_list();
		$this->load->model('Accountcash_model');
		$vdata['account_bank'] 			= $this->Accountcash_model->arrays("account_bank");
		$vdata['user_loan'] 			= $this->Accountcash_model->arrays("user_loan");
		$vdata['account_cash_status'] 	= $this->Accountcash_model->arrays("account_cash_status");
		$vdata['username'] 	= $username;
		$vdata['realname'] 	= $realname;
		$vdata['is_loan'] 	= $is_loan;
		$vdata['fstatus'] 	= $fstatus;
		$vdata['strtime'] 	= $strtime;
		$vdata['endtime'] 	= $endtime;
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'accountcash';
		
		$this->load->view('/sys_admin/account/accountcash_list', $vdata);
	}
	
	function accountcashexcel(){
		$username 	= $this->input->get('username');
		$realname 	= $this->input->get('realname');
		$is_loan 	= $this->input->get('is_loan');
		$fstatus 	= $this->input->get('fstatus');
		$strtime	= $this->input->get('strtime');
		$endtime	= $this->input->get('endtime');
		
		$data = array();
		if($username != ''){
			$data['where']['username'] = $username;
		}
		if($username != ''){
			$data['where']['realname'] = $realname;
		}
		if($is_loan != '' && $is_loan != -1){
			$data['where']['is_loan'] = $is_loan;
		}
		if($fstatus != '' && $fstatus != -1){
			$data['where']['fstatus'] = $fstatus;
		}
		if($strtime != ''){
			$data['where']['strtime'] = $strtime;
		}
		if($endtime != ''){
			$data['where']['endtime'] = $endtime;
		}
		$data['limit'] = 'all';
		$data['page_size']  = sys_admin_LIST_PAGE_SIZE;
		$data['page_start'] = 0;
		$this->load->model('Accountcash_model');
		$vdata = $this->Accountcash_model->get_accountcash_list($data);
		
		$this->load->model('Area_model', 'area');
		$area_list = $this->area->get_list();
		//print_r($area_list);return;
		
		$this->load->library('PHPExcel');
		$this->load->library('PHPExcel/IOFactory'); 
		
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setTitle("用户资金统计")->setDescription(COMPANY_NAME); 
		$user_loan 				 = $this->Accountcash_model->arrays("user_loan");
		$account_bank 				 = $this->Accountcash_model->arrays("account_bank");
		$account_cash_status 				 = $this->Accountcash_model->arrays("account_cash_status");
		$objPHPExcel->setActiveSheetIndex(0);
		$titles = array("ID", "用户名", "姓名", "用户类型", "银行", "省份", "城市", "开户行","帐号", "提现金额", "到账金额", 
						"手续费", "申请时间", "审核时间", "审核备注", "状态");
		
		for($i = 0; $i < count($titles); $i ++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $titles[$i]);
		}
		$accountcash_list = $vdata['accountcash_list'];
		for($i = 0; $i < count($accountcash_list); $i++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i+2, $accountcash_list[$i]['id']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $i+2, $accountcash_list[$i]['username']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $i+2, $accountcash_list[$i]['realname']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $i+2, $user_loan[$accountcash_list[$i]['is_loan']]);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $i+2, @$account_bank[$accountcash_list[$i]['bank']]);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $i+2, $accountcash_list[$i]['province']?$area_list[$accountcash_list[$i]['province']]:'');
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6, $i+2, $accountcash_list[$i]['city']?$area_list[$accountcash_list[$i]['city']]:'');
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(7, $i+2, $accountcash_list[$i]['branch']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(8, $i+2, $accountcash_list[$i]['account'].' ');
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(9, $i+2, $accountcash_list[$i]['total']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(10, $i+2, $accountcash_list[$i]['credited']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(11, $i+2, $accountcash_list[$i]['fee']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(12, $i+2, date('Y-m-d H:i:s', $accountcash_list[$i]['add_time']));
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(13, $i+2, date('Y-m-d H:i:s', $accountcash_list[$i]['verify_time']));
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(14, $i+2, $accountcash_list[$i]['verify_remark']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(15, $i+2, $account_cash_status[$accountcash_list[$i]['fstatus']]);
		}
		
		
		$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); 
	
		//发送标题强制用户下载文件 
		header('Content-Type: application/vnd.ms-excel'); 
		header('Content-Disposition: attachment;filename="用户提现.xls"'); 
		header('Cache-Control: max-age=0'); 
		
		$objWriter->save('php://output'); 
	}
	
	function verify(){
		$this->load->library('session');
		$adminUser = $this->session->userdata('admin_user');
		if(!isset($adminUser['user_id']) || !$adminUser['user_id']){
			echo "非法操作";
			exit;
		}
		$id 			= $this->input->post("id");
		$fstatus 		= $this->input->post("fstatus");
		$verify_remark	= $this->input->post("verify_remark");
		$this->load->model('Accountcash_model');
		$this->load->model('Account_model');
		$cash = $this->Accountcash_model->get_by_id($id);
		if($cash){
			if($cash['fstatus']!=0 || $cash['fstatus']==$fstatus){
				echo "请不要重复操作";
				exit;
			}
			if($fstatus == 2){
				$user_account = $this->Account_model->get_account($cash['user_id']);
				$log = array(
	  				'user_id' => $cash['user_id'],
	  				'ftype' => RECHARGE_FALSE,//提现失败
					'money' => $cash['total'],
	  				'total' => $user_account['total'],
	  				'use_money' => $user_account['use_money']+$cash['total'],
	  				'no_use_money' => $user_account['no_use_money']-$cash['total'],
	  				'collection' => $user_account['collection'],
	  				'to_user' => 1,
	  				'remark' => '提现审核未通过',
	  				'add_time' => time(),
	  				'addip' => $this->js->get_ip(),
	  			);
	  			$ret = $this->Account_model->addlog($log);
			}
			$data["fstatus"]			= $fstatus;
			$data["verify_userid"]      = $adminUser['user_id'];
			$data["verify_remark"]		= $verify_remark;
			$data["verify_time"]		= time();
			$data["verify_ip"]			= $this->js->get_ip();
			$ret = $this->Accountcash_model->update($id, $data);
			$this->load->model('log_model');
			$this->log_model->add_log('[审核][提现][ID=='.$id.']');
			echo "操作成功";
		}else{
			$this->load->model('log_model');$this->log_model->add_log('[审核][提现][错误][ID=='.$id.']');
			echo "数据错误";
		}
		
		/***
		$this->load->library('session');
		$adminUser = $this->session->userdata('admin_user');
		if(!isset($adminUser['user_id']) || !$adminUser['user_id']){
			echo "非法操作";
			exit;
		}
		$id 			= $this->input->post("id");
		$fstatus 		= $this->input->post("fstatus");
		$verify_remark	= $this->input->post("verify_remark");
		$this->load->model('Accountcash_model');
		$this->load->model('Account_model');
		$cash = $this->Accountcash_model->get_by_id($id);
		$this->load->model('User_model','user');
		$user_account = $this->Account_model->get_account($cash['user_id']);
		if($cash['fstatus'] == 0 && $fstatus  == 1 ){
			
			// ******* 20141227 by liub  资金托管：向宝付提现申请
			$this->load->library('baofooSdk/BaofooRequestService',0,'br');
			
			$user_msg = $this->user->get_realname_by_id($cash['user_id']);
			$config = Array();
			// 封装请求信息
			$config['order_id'] 	= self::createOrderId(6);// 订单ID(必须唯一)
			$config['user_id'] 		= $cash['user_id'];
			$config['user_name'] 	= $user_msg['realname'];
			$config['amount'] 		= $cash['total']-$cash['fee'];
			$config['fee'] 			= $cash['fee'];
			$config['fee_taken_on'] = FEE_TAKEN_ON;
			$config['bank_no'] 		= $cash['account'];
			
			$this->load->library('baofooSdk/dto/request/FoChargeDto',$config,'foChargeDto');
			$result 		= 	$this->br->	api_FoCharge($this->foChargeDto);	
		
			if($result['code'] !='CSD000'){
				$this->db->trans_strict(FALSE);
    			$this->db->trans_begin();
				
				echo "提现订单失败，请联系管理员";
				$data["fstatus"]			= 6;// 申请失败
				$data["verify_userid"]      = $adminUser['user_id'];
				$data["verify_remark"]		= $result['msg'];
				$data["verify_time"]		= time();
				$data["trade_no"]			= $config['order_id'];// 订单号
				$data["bf_result_code"]		= $result['code'];		// 宝付失败返回码
				$data["verify_ip"]			= $this->js->get_ip();
				$ret = $this->Accountcash_model->update($id, $data);
				
				$log = array(
	  				'user_id' => $cash['user_id'],
	  				'ftype' => RECHARGE_FALSE,//提现失败
					'money' => $cash['total'],
	  				'total' => $user_account['total'],
	  				'use_money' => $user_account['use_money']+$cash['total'],
	  				'no_use_money' => $user_account['no_use_money']-$cash['total'],
	  				'collection' => $user_account['collection'],
	  				'to_user' => 1,
	  				'remark' => '提现审核未通过',
	  				'add_time' => time(),
	  				'addip' => $this->js->get_ip(),
	  			);
	  			$ret = $this->Account_model->addlog($log);
				if ($this->db->trans_status() === TRUE) {
        			$this->db->trans_commit();
        			return 1;
   				 } else {
       		 		$this->db->trans_rollback();
       				 //@todo 异常处理部分
        			return 0;
   			 	}
			}
		}
		if($cash){
			if($cash['fstatus']!=0 || $cash['fstatus']==$fstatus){
				echo "请不要重复操作";
				exit;
			}
			$this->db->trans_strict(FALSE);
    		$this->db->trans_begin();
			if($fstatus == 2){
				$log = array(
	  				'user_id' => $cash['user_id'],
	  				'ftype' => RECHARGE_FALSE,//提现失败
					'money' => $cash['total'],
	  				'total' => $user_account['total'],
	  				'use_money' => $user_account['use_money']+$cash['total'],
	  				'no_use_money' => $user_account['no_use_money']-$cash['total'],
	  				'collection' => $user_account['collection'],
	  				'to_user' => 1,
	  				'remark' => '提现审核未通过',
	  				'add_time' => time(),
	  				'addip' => $this->js->get_ip(),
	  			);
	  			$ret = $this->Account_model->addlog($log);
			}
			$data["fstatus"]			= $fstatus;
			$data["verify_userid"]      = $adminUser['user_id'];
			$data["verify_remark"]		= $verify_remark;
			$data["verify_time"]		= time();
			$data["trade_no"]			= $config['order_id'];// 订单号
			$data["verify_ip"]			= $this->js->get_ip();
			$ret = $this->Accountcash_model->update($id, $data);
			$this->load->model('log_model');
			$this->log_model->add_log('[审核][提现][ID=='.$id.']');
			
			$ret = $this->Account_model->addlog($log);
			if ($this->db->trans_status() === TRUE) {
        		$this->db->trans_commit();
        		echo "操作成功";
        		return 1;
   			} else {
       		 	$this->db->trans_rollback();
       				//@todo 异常处理部分
       			echo "操作失败";
        		return 0;
   			 }
			
		}else{
			$this->load->model('log_model');$this->log_model->add_log('[审核][提现][错误][ID=='.$id.']');
			echo "数据错误";
		}
		*/
	}
	
	/** 到帐审核*/
	function review(){
		$id 			= $this->input->post("id");
		$fstatus 		= $this->input->post("fstatus");
		$review_remark	= $this->input->post("review_remark");
		
		$data["fstatus"]			= $fstatus;
  		$data["review_remark"]		= $review_remark;
  		$data["review_time"]		= time();
  		$data["review_ip"]			= $this->js->get_ip();
  		$this->load->model('Accountcash_model');
  		$this->load->model('Account_model');
  		$cash_obj = $this->Accountcash_model->get_by_id($id);
  		$time = time();
  		$ip = $this->js->get_ip();
		if($cash_obj['fstatus'] == 1){
			// 事务开启
			$this->db->trans_strict(FALSE);
   			$this->db->trans_begin();
			$ret = $this->Accountcash_model->update($id, $data);
			$cash = $this->Accountcash_model->get_by_id($id);
			if($ret){
	  			$account = $this->Account_model->get_account($cash['user_id']);
	  			if($fstatus == 4){
	  				$log = array(
		  				'user_id' => $cash['user_id'],
		  				'ftype' => RECHARGE_SUCCESS,// 到帐
		  				'total' => $account['total']-$cash['total'],
		  				'money' => $cash['total'],
		  				'use_money' => $account['use_money'],
		  				'no_use_money' => $account['no_use_money']-$cash['total'],
		  				'collection' => $account['collection'],
		  				'to_user' => 1,
		  				'remark' => '提现审核通过',
		  				'add_time' => $time,
		  				'addip' => $ip,
		  			);
		  			$ret = $this->Account_model->addlog($log);
		  			if ($ret){
		  				//提醒设置
		  				$this->notice_message($log['user_id'], '提现审核通过', '您于 '.date('Y-m-d H:i:s').' 提现：'.$cash['total'].'元审核通过，到账：'.$cash['credited'].'元，手续费：'.$cash['fee'].'元。');
						
		  			}
	  			}elseif($fstatus == 2){
	  				$log = array(
		  				'user_id' => $cash['user_id'],
		  				'ftype' => RECHARGE_FALSE,//提现失败
		  				'total' => $account['total'],
		  				'money' => $cash['total'],
		  				'use_money' => $account['use_money']+$cash['total'],
		  				'no_use_money' => $account['no_use_money']-$cash['total'],
		  				'collection' => $account['collection'],
		  				'to_user' => 1,
		  				'remark' => '提现到帐失败',
		  				'add_time' => $time,
		  				'addip' => $ip,
		  			);
		  			$ret = $this->Account_model->addlog($log);
	  			}
	  		}
	  		
			if ($this->db->trans_status() === TRUE) {
        		$this->db->trans_commit();
        		$this->load->model('log_model');$this->log_model->add_log('[审核][到账][成功][ID=='.$id.']');
        		echo "1";
    		} else {
      		 	 $this->db->trans_rollback();
      		 	 $this->load->model('log_model');$this->log_model->add_log('[审核][到账][失败][ID=='.$id.']');
      		 	 echo "操作失败！";
   			}
		}
	}
		
		/****
		$id 			= $this->input->post("id");
		$fstatus 		= $this->input->post("fstatus");
		$review_remark	= $this->input->post("review_remark");
	  	$this->load->model('Accountcash_model');
  		$this->load->model('Account_model');
		$cash_obj = $this->Accountcash_model->get_by_id($id);
	
		 //TODO 资金托管：验证
		$config['order_id'] = $cash_obj['trade_no'];
		$config['type'] = "6";
		$config['start_time'] = null;
		$config['end_time'] = null;
		$this->load->library('baofooSdk/dto/request/P2pQueryDto',$config,'P2pQueryDto');
		$this->load->library('baofooSdk/BaofooRequestService',0,'br');
		$resule = $this->br->api_P2pQuery($this->P2pQueryDto);
		$state = 0;// 0 未知，1 成功
		
		if($resule['result'] == null ){
			echo '宝付查询无此充值订单';
			exit;
		}
		if($resule['code'] == "CSD000"){
			if($resule['result'][0]['order'][0]['order_id'] == $config['order_id']  ){
				$state = $resule['result'][0]['order'][0]['state'];
			}else{
				echo '宝付查询无此提现订单';
				exit;
			}
		}else{
			echo '宝付查询失败，返回码：'.$resule['code'];
			exit;
		}
		if($state == -1){
						$time = time();
  			$ip = $this->js->get_ip();
			$this->load->model('Account_model');
  			$this->load->model('Accountcash_model');
  			$this->load->model('Account_model');
  			
  			// 事务开启
			$this->db->trans_strict(FALSE);
   			$this->db->trans_begin();

				$data["fstatus"]				= 6;// 提现失败
  			$data["review_remark"]	= "宝付：提现已失败";
  			$data["review_time"]		= $time;
  			$data["review_ip"]			= $ip;			
				$ret = $this->Accountcash_model->update($id, $data);
  			if($ret){
  				$cash = $this->Accountcash_model->get_by_id($id);
  				$account = $this->Account_model->get_account($cash['user_id']);
			 		$log = array(
			  			'user_id' => $cash['user_id'],
			  			'ftype' => RECHARGE_FALSE,//提现失败
			  			'total' => $account['total'],
			  			'money' => $cash['total'],
			  			'use_money' => $account['use_money']+ $cash['total'],
			  			'no_use_money' => $account['no_use_money']-$cash['total'],
			  			'collection' => $account['collection'],
			  			'to_user' => $cash['user_id'],
			  			'remark' => '宝付：提现已失败',
			  			'add_time' => $time,
			  			'addip' => $ip,
			  		);
			  	$return = $this->Account_model->addlog($log);
			  	if ($return){
			  		//提醒设置
					//$this->load->model('Redmanagement_model', 'redmanagement');
					//$this->redmanagement->send_red($cash['user_id'],9,"提现");
			  		$this->notice_message($log['user_id'], '宝付：提现已失败', '您于 '.date('Y-m-d H:i:s').' 提现：'.$cash['total'].'元失败，返账：'.$cash['credited'].'元。');
			  	}
  			}
  			// 事务提交
			if($this->db->trans_status() === TRUE) {
        		$this->db->trans_commit();
        		$this->load->model('log_model');
        		$this->log_model->add_log('[自动审核][提现到账][失败][ID=='.$id.']');
        		echo '宝付反馈结果:提现失败';
				exit;
    		} else {
      		 	$this->db->trans_rollback();
      		 	$this->load->model('log_model');
      		 	$this->log_model->add_log('[自动审核][提现到账][失败][ID=='.$id.']');
        		echo '宝付反馈结果:未知';
				exit;
   			}
   			
		}elseif($state == 5){
			$data["fstatus"]			= 7;
  			$data["review_remark"]		= '宝付反馈结果:转账中';
  			$data["review_time"]		= time();
  			$data["review_ip"]			= $this->js->get_ip();
				$ret = $this->Accountcash_model->update($id, $data);
				echo '宝付反馈结果:转账中';
				exit;
		}elseif($state == 0){
			echo '宝付反馈结果:未知';
			exit;
		}
		
		$data["fstatus"]			= 4;
  		$data["review_remark"]		= $review_remark;
  		$data["review_time"]		= time();
  		$data["review_ip"]			= $this->js->get_ip();
  	
  		$this->load->model('Account_model');
  		$time = time();
  		$ip = $this->js->get_ip();
  		
		if($cash_obj['fstatus'] == 1){
			
			 $this->db->trans_strict(FALSE);
   			 $this->db->trans_begin();
   			 
			$ret = $this->Accountcash_model->update($id, $data);
			$cash = $this->Accountcash_model->get_by_id($id);
			if($ret){
	  			$account = $this->Account_model->get_account($cash['user_id']);
	  			if($fstatus == 4){
	  				$log = array(
		  				'user_id' => $cash['user_id'],
		  				'ftype' => RECHARGE_SUCCESS,//提现审核通过
		  				'total' => $account['total']-$cash['total'],
		  				'money' => $cash['total'],
		  				'use_money' => $account['use_money'],
		  				'no_use_money' => $account['no_use_money']-$cash['total'],
		  				'collection' => $account['collection'],
		  				'to_user' => 1,
		  				'remark' => '提现成功',
		  				'add_time' => $time,
		  				'addip' => $ip,
		  			);
		  			$ret = $this->Account_model->addlog($log);
		  			if ($ret){
		  				//提醒设置
		  				//发放代金券
						$this->load->model('Redmanagement_model', 'redmanagement');
						$this->redmanagement->send_red($cash['user_id'],9,"提现");
		  				$this->notice_message($log['user_id'], '提现审核通过', '您于 '.date('Y-m-d H:i:s').' 提现：'.$cash['total'].'元审核通过，到账：'.$cash['credited'].'元，手续费：'.$cash['fee'].'元。');
						
		  			}
	  			}elseif($fstatus == 2){
	  				$log = array(
		  				'user_id' => $cash['user_id'],
		  				'ftype' => RECHARGE_FALSE,//提现失败
		  				'total' => $account['total'],
		  				'money' => $cash['total'],
		  				'use_money' => $account['use_money']+$cash['total'],
		  				'no_use_money' => $account['no_use_money']-$cash['total'],
		  				'collection' => $account['collection'],
		  				'to_user' => 1,
		  				'remark' => '提现审核未通过',
		  				'add_time' => $time,
		  				'addip' => $ip,
		  			);
		  			$ret = $this->Account_model->addlog($log);
	  			}
	  		}
	  		
	  		if ($this->db->trans_status() === TRUE) {
        		$this->db->trans_commit();
        		$this->load->model('log_model');$this->log_model->add_log('[审核][到账][成功][ID=='.$id.']');
        		echo "1";
        		return 1;
    		} else {
      		 	 $this->db->trans_rollback();
      		 	 $this->load->model('log_model');$this->log_model->add_log('[审核][到账][失败][ID=='.$id.']');
      		 	 echo "操作失败！";
        		//@todo 异常处理部分
        		return 0;
   			}
		}
		*/
	

	function batchreview(){
		$ids 			= $this->input->post("ids");
		$id_list = explode(',', $ids);
		$fstatus = 4;
		$ip = $this->js->get_ip();
		$data["fstatus"]			= $fstatus;
  		$data["review_remark"]		= "审核通过";
  		$data["review_time"]		= time();
  		$data["review_ip"]			= $ip;
  		$this->load->model('Accountcash_model');
  		$this->load->model('Account_model');
  		$this->load->model('Message_model');
  		$time = time();
		foreach ($id_list as $id){
			$cash_obj = $this->Accountcash_model->get_by_id($id);
			if($cash_obj['fstatus'] == 1){
				$this->db->trans_strict(FALSE);
				$this->db->trans_begin();//事务开始
				$ret = $this->Accountcash_model->update($id, $data);
				$cash = $this->Accountcash_model->get_by_id($id);
				if($ret){
		  			$account = $this->Account_model->get_account($cash['user_id']);
		  			if($fstatus == 4){
		  				$log = array(
			  				'user_id' => $cash['user_id'],
			  				'ftype' => RECHARGE_SUCCESS,//提现审核通过
			  				'total' => $account['total']-$cash['total'],
			  				'money' => $cash['total'],
			  				'use_money' => $account['use_money'],
			  				'no_use_money' => $account['no_use_money']-$cash['total'],
			  				'collection' => $account['collection'],
			  				'to_user' => 1,
			  				'remark' => '提现审核通过',
			  				'add_time' => $time,
			  				'addip' => $ip
			  			);
			  			$ret = $this->Account_model->addlog($log);
			  			if ($ret){
			  				//提醒设置
			  				$this->notice_message($log['user_id'], '提现审核通过', '您于 '.date('Y-m-d H:i:s').' 提现：'.$cash['total'].'元审核通过，到账：'.$cash['credited'].'元，手续费：'.$cash['fee'].'元。');
							
			  			}
		  			}elseif($fstatus == 2){
		  				$log = array(
			  				'user_id' => $cash['user_id'],
			  				'ftype' => RECHARGE_FALSE,//提现失败
			  				'total' => $account['total'],
			  				'money' => $cash['total'],
			  				'use_money' => $account['use_money']+$cash['total'],
			  				'no_use_money' => $account['no_use_money']-$cash['total'],
			  				'collection' => $account['collection'],
			  				'to_user' => 1,
			  				'remark' => '提现审核未通过',
			  				'add_time' => $time,
			  				'addip' => $ip
			  			);
			  			$ret = $this->Account_model->addlog($log);
		  			}
		  		}
				if($this->db->trans_status() === TRUE){
					$this->db->trans_commit();
				}else{
					$this->db->trans_rollback();
		  			echo "操作失败！";
		  			exit;
				}
			}
			$ret = 0;
		}		
		echo "1";
	}
	

	function deduct_view(){
		$this->load->model('Accountlog_model');
		$var_account_deduct_type = $this->Accountlog_model->arrays("var_account_deduct_type");
		$vdata['var_account_deduct_type'] = $var_account_deduct_type;
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'deduct';
		
		$this->load->view('/sys_admin/account/accountdeduct', $vdata);
	}

	
	function deduct(){
		$ret = array(
			'msg' => '',
			'url' => '',
		);
		$username 		= $this->input->post('username');
		$money 			= $this->input->post('money');
		$deduct_type 	= $this->input->post('deduct_type');
		$remark 		= $this->input->post('remark');
		
		$user_id = 0; 
		if(!$username){
			$ret['msg'] = '请填写用户名';
		}elseif(!$this->myformcheck->IsDouble($money)){
			$ret['msg'] = '请填写有效的金额';
		}elseif(!$remark){
			$ret['msg'] = '请填写备注';
		}else{
			$this->load->model('User_model');
			$user_id = $this->User_model->get_userid_by_username($username);
			if($user_id < 1){
				$ret['msg'] = '用户名不存在';
			}
		}
		if($ret['msg']){
			echo json_encode($ret);exit;
		}
		$admin = $this->session->userdata('admin_user');
		$data['admin_user_id']  = $admin['user_id'];
		$data['user_id'] 		= $user_id;
		$data['account'] 		= $money;
		$data['ftype'] 	 		= $deduct_type;
		$data['remark']  		= $remark;
		$data['ip']  			= $this->js->get_ip();
		
		$this->load->model('Accountcash_model');
		$flag = $this->Accountcash_model->deduct($data);
		if($flag == 1){
			$ret['msg'] = '费用扣除成功';
			$this->load->model('log_model');$this->log_model->add_log('[添加][费用扣除]'.'[用户名:'.$username.',金额:'.$money.']');
		}else{
			$ret['msg'] = '费用扣除失败';
			$this->load->model('log_model');$this->log_model->add_log('[添加][费用扣除]'.'[用户名:'.$username.',金额:'.$money.']');
		}
   		echo json_encode($ret);
	}
	
	
	//***** by liub 根据交易类型生成订单ID
	private function createOrderId($type){
		// 时间戳
		$orderid = time();
		// 生成3位随机数字
		$num = create_num(3);// 模拟毫秒数
		// 生成订单id
		$orderid =$type.'_'.$orderid.$num;
		
		return $orderid;
	}
>>>>>>> d47623b610d79a4eeb489500830c00d70933f414
}